package net.rossillo.spring.web.servlet.http;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;

/**
 * Provides an HTTP servlet request wrapper for Facebook requests.
 * 
 * @author Scott Rossillo
 *
 */
public class FacebookHttpServletRequest extends HttpServletRequestWrapper {

	private final boolean applicationAdded;
	private final String browserRequestMethod;
	
	private final String authToken;
	private final String sessionKey;
	
	private final String xid;
	private final Long uid;
	private final List<Long> friendUids = new ArrayList<Long>();
	
	/**
	 * Constructs a new Facebook request wrapper for the given request.
	 * 
	 * @param request the <code>HttpServletRequest</code> to wrap
	 */
	public FacebookHttpServletRequest(HttpServletRequest request) {
		
		super(request);
		
		authToken = request.getParameter("auth_token");
		sessionKey = request.getParameter("fb_sig_session_key");
		
		applicationAdded = !"0".equals(request.getParameter("fb_sig_added")) ? true : false;
		browserRequestMethod = request.getParameter("fb_sig_request_method");
		
		String sUid = request.getParameter("fb_sig_user");
		uid = sUid != null ? new Long(sUid) : null;
		
		xid = request.getParameter("fb_sig_xid");
	}


	public String getAuthToken() {
		return authToken;
	}


	public String getSessionKey() {
		return sessionKey;
	}


	/**
	 * Returns true if the current user has added this application.
	 * 
	 * @return <code>true</code> if the current user has added this 
	 * application; <code>false</code> otherwise
	 */
	public boolean isApplicationAdded() {
		return applicationAdded;
	}


	/**
	 * Returns the browser request method, typically an HTTP GET or POST.
	 * Facebook always POSTs to applications but relays the actual browser
	 * request method to the application.
	 * 
	 * @return the HTTP request method invoked by the client's browser
	 * to Facebook's server
	 */
	public String getBrowserRequestMethod() {
		return browserRequestMethod;
	}


	public String getXid() {
		return xid;
	}


	public Long getUid() {
		return uid;
	}


	public List<Long> getFriendUids() {
		return friendUids;
	}
	
}
